<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class AppointCom{
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }
    function addAppoint($customerid,$reservetimeid,$time,$address,$usertypeid,$floor,$areaid,$streetid,$communityid,$serviceid,$username,$codeid,$sex,$age,$mobile){
        try {
            $create_time = time();
            $sql = "insert into appoint(customer_id,reservetime_id,time,address,usertype_id,floor,area_id,street_id,community_id,service_id,username,codeid,sex,age,mobile,create_time"
                    . ") values({$customerid},{$reservetimeid},'{$time}','{$address}',{$usertypeid},{$floor},{$areaid},{$streetid},{$communityid},{$serviceid},'{$username}','{$codeid}',{$sex},{$age},'{$mobile}','{$create_time}');SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['@@Identity'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAppointDesc($orderid){
        try {
            $sql = "select * from appoint where order_id={$orderid} order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAppointById($id){
        try {
            $sql = "select * from appoint where id={$id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAppointlist($customerid){
        try {
            $sql = "select * from appoint where customer_id={$customerid} order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumByRtime(){
        try{
            $sql = "select reservetime_id,count(1) from appoint group by reservetime_id";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function checkOrderTime($time,$community,$rtime){
        try{
            $sql = "select id from appoint where time='{$time}' and community_id={$community} and reservetime_id={$rtime}";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAppointHistory($customerid){
        try{
            $sql = "select * from appoint where customer_id={$customerid} order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAppointByCid($community){
        try {
            $sql = "select * from appoint where community_id in ({$community}) order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function changeStatus($id,$status){
        try {
            $sql = "update appoint set status={$status} where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllwNum($community){
        try{
            $sql = "select count(1) from appoint where community_id in ({$community})";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumByAge(){
        try{
            $sql = "select age,count(1) from appoint group by age";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumwByAge($community){
        try{
            $sql = "select age,count(1) from appoint where community_id in ({$community}) group by age";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumByFloor(){
        try{
            $sql = "select floor,count(1) from appoint group by floor";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumwByFloor($community){
        try{
            $sql = "select floor,count(1) from appoint where community_id in ({$community}) group by floor";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumBySex(){
        try{
            $sql = "select sex,count(1) from appoint group by sex";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    function getNumwBySex($community){
        try{
            $sql = "select sex,count(1) from appoint where community_id in ({$community}) group by sex";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    function getNumByArea($area){
        try{
            $sql = "select count(1) from appoint where area_id={$area}";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumByUsertype($usertype){
        try{
            $sql = "select count(1) from appoint where usertype_id={$usertype}";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumwByUsertype($usertype,$community){
        try{
            $sql = "select count(1) from appoint where usertype_id={$usertype} and community_id in ({$community})";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    
    function getAllNum(){
        try{
            $sql = "select count(1) from appoint";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAllwAppoint($community,$minlimit, $limitnum){
        try{
            $sql = "select * from appoint where community_id in ({$community}) order by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllAppoint($minlimit, $limitnum){
        try{
            $sql = "select * from appoint order by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function deleteAppoint($id){
        try{
            $sql = "delete from appoint where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getCountByTime($time){
        try{
            $sql = "select count(1) from appoint where create_time>{$time}";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getwCountByTime($community,$time){
        try{
            $sql = "select count(1) from appoint where create_time>{$time} and community_id in ({$community})";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
}
